package com.bw.module_service.ui

import android.os.Bundle
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.bw.lib_common.arouter.Router
import com.bw.lib_common.base.BaseActivity
import com.bw.lib_storage.mmkv.AppMMKV.mmkv
import com.bw.lib_storage.mmkv.AuthMMKV
import com.bw.module_service.R
import com.bw.module_service.adapter.PostCarAdapter
import com.bw.module_service.databinding.ActivityPostCarBinding
import com.bw.module_service.viewmodel.ServiceViewModel
import com.google.gson.Gson
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody

/*
这是添加车辆界面！！！！
 */
@AndroidEntryPoint
@Route(path = Router.Ui.PostCarActivity)
class PostCarActivity : BaseActivity<ServiceViewModel, ActivityPostCarBinding>() {

    override val mLayoutResId: Int
        get() = R.layout.activity_post_car

    //车牌默认
    var str: String="京"

    private val mViewModel: ServiceViewModel by viewModels()
    private var mAdapter: PostCarAdapter = PostCarAdapter()


    override fun initData() {
        mViewModel.getProvinces()

        mBinding.postCaradd.setOnClickListener({
            val map=mutableMapOf<String, Any>()
            if (mBinding.inputId.text.toString()==null){
                Toast.makeText(this,"请填写车牌号", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if (mmkv.getValueSize("username").toString()==null|| mmkv.getValueSize("userid").toString()==null){
                Toast.makeText(this,"请先登录", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if (str==""){
                Toast.makeText(this,"请选择省份简称", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if (mBinding.inputId.text.toString().any{it!in 'A'..'Z' && it!in 'a'..'z' && it!in '0'..'9'}){
                Toast.makeText(this,"请检查车牌号", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            map["licensePlate"]= str+mBinding.inputId.text.toString()
            map["ownerName"]= AuthMMKV.username
            map["provinceAbbreviation"]= str
            map["userId"]= AuthMMKV.userid
            val json= Gson().toJson(map)
            val mediaType = "application/json".toMediaTypeOrNull()
            val body = json.toRequestBody(mediaType)
            mViewModel.addCar(body)
        })


        mAdapter.setOnItemClickListener { adapter, view, position ->
            str = mAdapter.data[position].abbreviation
            mBinding.carIp.text =str
        }

        //返回
        mBinding.back.setOnClickListener {
            finish()
        }

        //跳转到扫描
//        mBinding.camera.setOnClickListener {
//            ARouter.getInstance().build(Router.Ui.SMActivity).navigation()
//        }

    }

    override fun initView() {
        mBinding.rv.layoutManager = GridLayoutManager(this,7)
        mBinding.rv.adapter = mAdapter


    }

    override fun initListener() {
        mViewModel._provincesLiveData.observe(this){
            mAdapter.setList(it)
            mAdapter.notifyDataSetChanged()
        }

        mViewModel._addCarLiveData.observe(this) {
            Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show()
            finish()
        }
    }

}